package tool;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import model.ConfigModel;

public class ParsingMonitor implements Runnable {

	private static Set<String> parsingSet = new HashSet<String>();

	private String dataFolder;
	private String select;

	public ParsingMonitor(String dataFolder, String select) {
		this.dataFolder = dataFolder;
		this.select = select;
	}

	public void run() {
		if (parsingSet.contains(select)) {
			return;
		}

		parsingSet.add(select);
		
		try {
			parseFile();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("The format of configuration file \"" + select
					+ "\" is not valid");
			e.printStackTrace();
		}

		parsingSet.remove(select);
	}
	
	public static boolean contains(String select){
		return parsingSet.contains(select);
	}

	public void parseFile() throws Exception {
		ConfigModel model = DataFileHandler.parseDataFile(dataFolder
				+ DataFileHandler.DATA_ORIGIN + "/" + select);

		File folder = new File(dataFolder
				+ DataFileHandler.DATA_PARSED + "/" + select);
		folder.mkdirs();
		model.store(folder.getAbsolutePath());
	}
}
